import baseapi from '@/api/base'
import { getDataByParameter, getObjetInfoByPid, getCountByParameter, saveObjetInfo, deleteObjetInfo } from '@/utils/baserequest'
import storage from 'store'

const Base64 = require('js-base64').Base64

const api = {
  appContext: baseapi.webmanagerUrl,
  appAppId: baseapi.webmanager_app,
  appServer: 'webInfo',
  findListByOrgPid: 'webInfo/by/orgPid/',
  findCountByOrgPid: 'webInfo/countBy/orgPid/',
  findListLikeExample: 'webInfo/by/findLikeExample/',
  findCountLikeExample: 'webInfo/countLikeExample/'
}

export default api

// 得到单位成员 分页
export function getListByOrgPid (headers, paginations) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const parameters = {
    orgPid: storage.get('org'),
    pageSize: paginations.pageSize,
    currentPage: paginations.current
  }
  const url = api.appContext + api.findListByOrgPid
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, parameters, allheaders)
}

// 得到单位成员总数
export function getCountByOrgPid (headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const url = api.appContext + api.findCountByOrgPid
  const allheaders = Object.assign(headers, appIdHeaders)
  return getCountByParameter(url, appIdHeaders.org, allheaders)
}

// 条件查询列表
export function getListLikeExample (example, headers, paginations) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const parameters = {
    example: Base64.encode(JSON.stringify(example)),
    pageSize: paginations.pageSize,
    currentPage: paginations.current
  }
  const url = api.appContext + api.findListLikeExample
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, parameters, allheaders)
}

// 条件查询总数
export function getCountLikeExample (example, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const e = Base64.encode(JSON.stringify(example))
  const url = api.appContext + api.findCountLikeExample
  const allheaders = Object.assign(headers, appIdHeaders)
  return getCountByParameter(url, e, allheaders)
}

// 得到人员信息
export function getInfoByPid (parameter, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer
  const allheaders = Object.assign(headers, appIdHeaders)
  return getObjetInfoByPid(url, parameter, allheaders)
}

// id == 0 add     post
// id != 0 update  put
// 保存人员信息
export function saveInfo (parameter, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return saveObjetInfo(url, parameter, allheaders)
}

// 删除单位和成员关系
export function delInfo (parameter, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return deleteObjetInfo(url, parameter, allheaders)
}
